<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    request.setAttribute("basePath",basePath);
%>
<!DOCTYPE>
<html>
<head>
    <title>优惠套餐管理</title>
    <%@ include file="/resources/js/easyui/taglibs.jsp"%>
    <%@ include file="/resources/js/easyui/_easyui.jsp"%>
    <%@ include file="/resources/js/easyui/_message.jsp"%>
    <%@ include file="/resources/js/easyui/_viewJS.jsp"%>
</head>

<body>

<div id="tb" style="padding:5px;height:auto;">

    <div id="button">
        <a href="javascript:void(0)" class="easyui-linkbutton" plain="true" iconCls="icon-add" id="addWinBtn">新增</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" plain="true" iconCls="icon-edit" id="editWinBtn">修改</a>
        <a href="javascript:void(0)" class="easyui-linkbutton" plain="true" iconCls="icon-remove" id="delBtn">删除</a>
    </div>

</div>

<table id="dg"
       title="优惠套餐管理"
       fitColumns="true"
       singleSelect="true"
       data-options="
       pageSize:10,
       toolbar:'#tb',
       pagination:true,
       collapsible:true,
       url:'${basePath}web/discount/list.json',
       method:'GET'">
    <thead>

    <tr>
        <th data-options="field:'discountid',checkbox:true,width:10" ></th>
        <th data-options="field:'discountname' ,width:150" align="center">套餐名称</th>
        <th data-options="field:'money' ,width:150" align="center">金额</th>
        <th data-options="field:'coin' ,width:150" align="center">设备余币</th>
    </tr>

    </thead>
</table>

<!-- 添加窗体s -->
<div id="addWin"
     class="easyui-window"
     minimizable="false"
     collapsible="false"
     title="明细窗口"
     data-options="closed:true,modal:true,top:20"
     style="width:600px;height:400px;">
    <div id="addWinDetailTable" class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center',border:false"></div>
        <div data-options="region:'south',border:false" style="text-align:center;padding:5px 5px 5px 0;background-color: #F5F5F5;border-top: solid 1px #E6E6E6;">
            <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" id="addSubBtn">提交</a>
            <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" id="closeAddWinBtn">关闭</a>
        </div>
    </div>
</div>
<!-- 添加窗体e -->

<!-- 修改窗体s -->
<div id="editWin"
     class="easyui-window"
     minimizable="false"
     collapsible="false"
     title="明细窗口"
     data-options="closed:true,modal:true,top:20"
     style="width:600px;height:400px;">
    <div id="editWinDetailTable" class="easyui-layout" data-options="fit:true">
        <div data-options="region:'center',border:false"></div>
        <div data-options="region:'south',border:false" style="text-align:center;padding:5px 5px 5px 0;background-color: #F5F5F5;border-top: solid 1px #E6E6E6;">
            <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" href="javascript:void(0)" id="editSubBtn">提交</a>
            <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" href="javascript:void(0)" id="closeEditWinBtn">关闭</a>
        </div>
    </div>
</div>
<!-- 修改窗体e -->

</body>

<script type="text/javascript">
    function View(){
        var that = this;

        that.events = [
            { sel : '#queryDataBtn', act : 'click', func : 'queryData' },
            { sel : '#addWinBtn', act : 'click', func : 'addWin' },
            { sel : '#addSubBtn', act : 'click', func : 'addSub' },
            { sel : '#closeAddWinBtn', act : 'click', func : 'closeAddWin' },
            { sel : '#editWinBtn', act : 'click', func : 'editWin' },
            { sel : '#editSubBtn', act : 'click', func : 'editSub' },
            { sel : '#closeEditWinBtn', act : 'click', func : 'closeEditWin' },
            { sel : '#delBtn', act : 'click', func : 'delSub' }
        ];

        that.$dataGrid = $('#dg');
        that.$addWin = $('#addWin');
        that.$addWinDetailTable = $("#addWinDetailTable");

        that.$editWin = $('#editWin');
        that.$editWinDetailTable = $("#editWinDetailTable");

        that.dataGridOption = {};

        $.extend(that.dataGridOption, ViewHelper.template.gridOption);

        that.$dataGrid.datagrid(that.dataGridOption);

        that.queryData = function () {
            that.$dataGrid.datagrid(that.dataGridOption);
        };

        that.addWin = function(){
            var url = "${basePath}web/discount/addView";
            that.$addWinDetailTable.layout('panel','center').find('iframe').remove();
            that.$addWinDetailTable.layout('panel','center').append("<iframe id='add_iframe' name='info_iframe' width='100%' height='100%' frameborder='0'  src='"+url+"'></iframe>");
            that.$addWin.dialog('open');
        };

        that.addSub = function(){
            $("#add_iframe")[0].contentWindow.add();
        };

        that.closeAddWin = function(result){
            if(result==1){
                $.messager.alert('提示','保存成功!','info');
            }else if(result==0){
                $.messager.alert('提示','保存失败!','error');
            }
            that.$dataGrid.datagrid('reload');
            that.$addWin.window('close');
        };

        that.editWin = function(){
            var row = that.$dataGrid.datagrid('getSelected');
            if(row == null){
                $.fn.window.defaults.top=50;
                $.messager.alert('警告','请选择一条',"error");
                return ;
            }
            var url = "${basePath}web/discount/"+row.discountid+"/editView";
            that.$editWinDetailTable.layout('panel','center').find('iframe').remove();
            that.$editWinDetailTable.layout('panel','center').append("<iframe id='edit_iframe' name='info_iframe' width='100%' height='100%' frameborder='0'  src='"+url+"'></iframe>");
            that.$editWin.dialog('open');
        };

        that.editSub = function(){
            $("#edit_iframe")[0].contentWindow.edit();
        };

        that.closeEditWin = function(result){
            if(result==1){
                $.fn.window.defaults.top=50;
                $.messager.alert('提示','保存成功!','info');
            }else if(result==0){
                $.fn.window.defaults.top=50;
                $.messager.alert('提示','保存失败!','error');
            }
            that.$dataGrid.datagrid('reload');
            that.$editWin.window('close');
        };

        that.delSub = function(){
            var row = that.$dataGrid.datagrid('getSelected');
            if(row == null){
                $.fn.window.defaults.top=50;
                $.messager.alert('警告','请选择需要删除的数据');
                return ;
            }
            var confirm;

            $.fn.window.defaults.top=50;
            confirm = window.confirm('删除后数据不能恢复，确定要删除吗？');
            if(confirm){
                $.ajax({
                    url: '${basePath}web/discount/'+row.discountid+'/delete',
                    type: 'post',
                    async: false,
                    traditional: true,
                    error: function () {
                        $.fn.window.defaults.top=50;
                        $.messager.alert('提示', '删除失败', 'error');
                    },
                    success: function () {
                        $("#dg").datagrid('reload');
                        $.fn.window.defaults.top=50;
                        $.messager.alert('提示', '删除成功', 'info');
                    }
                });
            }
        };

        EventHandler.call(that, that.events);

        return that;
    }

    var view;

    $(function(){
        inherit(View, EventHandler);
        view = new View();
    });

    function formatterInfoType(value,row,index){
        if(row.infotype==1){
            return "启动";
        }else if(row.infotype==2){
            return "关闭";
        }else if(row.infotype==3){
            return "警告";
        }else{
            return "故障";
        }
    }

    function formatterDate(value,row,index){
        return formatDateBoxFull(value);
    }

</script>

</html>